package com.tourism.website.articlepersonal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourism.website.articlepersonal.entity.ArticleCategory;
import com.tourism.website.articlepersonal.mapper.ArticleCategoryMapper;
import com.tourism.website.articlepersonal.service.IArticleCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourismwebsite.articledisplay.dto.ArticleCategoryDTO;
import com.tourismwebsite.articlemanage.dto.ArticleCategoryManageDTO;
import com.tourismwebsite.articlepersonal.dto.ArticleCategoryPersonalDTO;
import com.tourismwebsite.articlepersonal.query.ArticlePersonalQuery;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kevin
 * @since 2024-06-13
 */
@Mapper(componentModel = "spring")
interface MsCategoryToAMapper {
    ArticleCategoryPersonalDTO ArticleToArticleManageDTO(ArticleCategory articleCategory);
}
@Service
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryMapper, ArticleCategory> implements IArticleCategoryService {
    @Resource
    MsCategoryToAMapper msCategoryToAMapper;
    @Resource
    ArticleCategoryMapper articleCategoryMapper;

    @Override
    public List<ArticleCategoryPersonalDTO> queryCategoryToArticleID(Integer categoryId) {
        LambdaQueryWrapper<ArticleCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(ArticleCategory::getArticleId);
        wrapper.eq(ArticleCategory::getCategoryId, categoryId);
        List<ArticleCategory> articleCategories = baseMapper.selectList(wrapper);
        List<ArticleCategoryPersonalDTO> articleCategoryPersonalDTOS = articleCategories.stream()
                .map(ArticleCategory -> msCategoryToAMapper.ArticleToArticleManageDTO(ArticleCategory))
                .collect(Collectors.toList());
        return articleCategoryPersonalDTOS;
    }

    @Override
    public boolean saveCategory(Integer articleId, Integer categoryId) {
        int pid = articleCategoryMapper.queryCategoryPid(categoryId); // 使用 Integer 类型接收，允许为 null

        if (pid !=0 ){
            int result1 = articleCategoryMapper.insertCategory(articleId,pid);
            int result = articleCategoryMapper.insertCategory(articleId,categoryId);
            return result >0 && result1>0;
        }

        // 插入文章类别关联记录
        int result = articleCategoryMapper.insertCategory(articleId,categoryId);


        return result >0;
    }





}
